package com.ruoyi.system.utils;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpUtils {
    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

    public static String postXml(String url, String xmlData) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        
        try {
            // 设置请求头
            httpPost.setHeader("Content-Type", "text/xml; charset=UTF-8");
            
            // 设置请求体
            StringEntity entity = new StringEntity(xmlData, "UTF-8");
            httpPost.setEntity(entity);
            
            // 发送请求并获取响应
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            } finally {
                response.close();
            }
        } finally {
            httpClient.close();
        }
    }
} 